﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using RoofRunner.Base;
using RoofRunner.Actors;
using RoofRunner.GameComponents;
using RoofRunner.Base.Camera;
using RoofRunner.Actors.ActorComponents.Rendering;
using RoofRunner.Actors.ActorComponents;
using RoofRunner.GameComponents.Rendering;
using RoofRunner.GameComponents.Controller;

namespace RoofRunner
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class RoofRunnerGame : Microsoft.Xna.Framework.Game
    {
        #region Fields
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;
        #endregion // Fields


        #region Properties
        public ICamera Camera { get; set; }


        public ActorMap Actors { get; set; }
        public static  RenderWorld RenderWorld { get; set; }
        public static GameWorld GameWorld { get; set; }
        #endregion // Properties


        #region Constructors
        public RoofRunnerGame()
        {
            _graphics = new GraphicsDeviceManager(this);
            
            this.IsFixedTimeStep = false;
            _graphics.SynchronizeWithVerticalRetrace = false;

            Content.RootDirectory = "Content";
        }
        #endregion // Constructors


        #region Methods
        #region GameMethods-Implementations
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            //_graphics.SynchronizeWithVerticalRetrace = false;
            _graphics.IsFullScreen = false;
            _graphics.PreferredBackBufferWidth = 1280;
            _graphics.PreferredBackBufferHeight = 720;
            _graphics.PreferredBackBufferFormat = SurfaceFormat.Color;
            _graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
            _graphics.PreferMultiSampling = true;
            _graphics.ApplyChanges();


            Camera = new BasicCamera();
            Camera.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                GraphicsDevice.Viewport.AspectRatio, 1.0f, 100.0f);
            
            this.Actors = new ActorMap();
            GameWorld = new GameWorld(this);
            RenderWorld = new RenderWorld(this);

            this.Components.Add(GameWorld);
            this.Components.Add(RenderWorld);

            this.Components.Add(new DebugMovementController(this));
            this.Components.Add(new DebugInfoComponent(this));

            base.Initialize();
        }


        #endregion // GameMethods-Implementations


        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.

            
            #region Textured-Cubes
            int max = 55;
            for(int i = 0; i < max; i++)
            {
                Vector3 pos = new Vector3(((i % 5) - 2) * 4, 0, (i / 5 - 3) * -4);
                var a = new Actor(pos);
                a.AddComponent(new ModelRenderComponent("Models\\cube", "Textures\\crate"));
                this.Actors.Add(a);
            }
            #endregion // Textured-Cubes


            #region Transparent-Objects
            Actor aTransparent1 = new Actor(new Vector3(5, 5, -10));
            aTransparent1.AddComponent(new ModelRenderComponent("Models\\cube", null, new Color(1, 1, 1, 128)));
            this.Actors.Add(aTransparent1);

            Actor aTransparent2 = new Actor(new Vector3(5, 5, -10));
            aTransparent2.Scaling *= 2;
            aTransparent2.AddComponent(new ModelRenderComponent("Models\\cube", null, new Color(1, 1, 1, 128)));
            this.Actors.Add(aTransparent2);
            #endregion // Transparent-Objects
            

            #region LOD-Object
            Actor aLOD1 = new Actor(new Vector3(-5, 5, -5));
            var lodMRC = new ModelRenderComponent("Alpha\\AlphaDemo", null);
            var lodCom = new LODComponent(lodMRC, new Dictionary<float, string> { { 0, "Alpha\\AlphaDemo" }, { 15, "Models\\cube" } });
            aLOD1.AddComponent(lodMRC);
            aLOD1.AddComponent(lodCom);
            this.Actors.Add(aLOD1);
            #endregion // LOD-Object


            #region Sky
            Actor sky = new Actor();
            sky.AddComponent(new SkyComponent());
            this.Actors.Add(sky);
            #endregion // Sky

            #region Skull
            Skull s = new Skull(new Vector3(10,3,10));
            this.Actors.Add(s);
            #endregion // Skull



            this.Actors.LoadContent(this.Content);
            _spriteBatch = new SpriteBatch(GraphicsDevice);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            // TODO: Add your update logic here

            base.Update(gameTime);
            this.Camera.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
        }
        #endregion // Methods
    }
}
